# import numpy as np
# def auc_2(y_true,y_pred):
#     idx = np.array(y_pred).argsort()[::-1]
#     y_true1 = np.array(y_true)[idx]
#     y_pred1 = np.array(y_pred)[idx]
#     pos,neg,k,pos_batch,neg_batch = 0,0,0,0,0
#     i = 0
#     while i<len(y_true1):
#         j = i
#         while j<len(y_true1) and y_pred1[j] == y_pred1[i]:
#             j+=1
#         pos_batch = sum(y_true1[i:j])
#         neg_batch = j-i-pos_batch
#         k += neg_batch*(pos+0.5*pos_batch)
#         pos+=pos_batch 
#         neg+=neg_batch
#         i = j
#     return k/(pos*neg)


# # label = [1,0,0,0,1,0,1,0]
# # pre = [0.9, 0.8, 0.3, 0.1, 0.4, 0.9, 0.66, 0.7]
# # print(auc(label, pre))

# from sklearn.metrics import roc_curve, auc
# def auc_0(label,pred):
#     fpr, tpr, th = roc_curve(label, pred , pos_label=1)
#     return auc(fpr, tpr)



# import numpy as np
# def auc_2(y_true,y_pred):
#     idx = np.argsort(y_pred)[::-1]
#     y_true = y_true[idx]
#     y_pred = y_pred[idx]
#     pos,neg,k = 0,0,0
#     i,n = 0,len(y_true)
#     while i<n:
#         j=i+1
#         while j<n and y_pred[j]==y_pred[i]:
#             j+=1
#         pos_batch = np.sum(y_true[i:j])
#         neg_batch = j-i-pos_batch 
#         k += neg_batch*(0.5*pos_batch + pos)
#         pos += pos_batch 
#         neg += neg_batch 
#         i = j 
#     return k/(pos*neg)

import numpy as np
def auc(y_true,y_pred):
    idx = np.argsort(y_pred)[::-1]
    y_true,y_pred = y_true[idx],y_pred[idx]
    k,pos,neg = 0,0,0
    for i in range(len(y_true)):
        if y_true[i] == 1:
            pos+=1
        else:
            neg+=1 
            k+=pos 
    return k/(pos*neg)

def auc_2(y_true,y_pred):
    idx = np.argsort(y_pred)[::-1]
    y_true,y_pred = y_true[idx],y_pred[idx]
    pos,neg,pos_batch,neg_batch,k = 0,0,0,0,0
    i,j = 0,0
    while i<len(y_true):
        j = i
        while j<len(y_true) and y_pred[j]==y_pred[i]: #***pred
            j+=1 
        pos_batch = np.sum(y_true[i:j])
        neg_batch = j-i-pos_batch 
        k += neg_batch*(pos+0.5*pos_batch)
        pos+=pos_batch 
        neg+=neg_batch 
        i = j
    return k/(pos*neg)





# def auc_2(y_true,y_pred):
#     idx = np.argsort(y_pred)[::-1]
#     y_true,y_pred = y_true[idx],y_pred[idx]
#     k,pos,neg = 0,0,0
#     i,j = 0,0
#     while j<len(y_true):
#         while j<len(y_true) and y_pred[j]==y_pred[i]:
#             j+=1
#         pos_batch = sum(y_true[i:j])
#         neg_batch = j-i-pos_batch 
#         k+=(pos+0.5*pos_batch)*neg_batch
#         pos+=pos_batch 
#         neg+=neg_batch 
#         i=j 
#     return k/(pos*neg)


    
y_true = np.array([1,1,0])
y_pred = np.array([0.5,0.5,0.6])
print(auc(y_true,y_pred))
# print(auc_0(y_true,y_pred))
print(auc_2(y_true,y_pred))

y_true = np.array([1,1,0])
y_pred = np.array([0.5,0.7,0.6])
print(auc(y_true,y_pred))
# print(auc_0(y_true,y_pred))
print(auc_2(y_true,y_pred))

y_true = np.array([1,1,0])
y_pred = np.array([0.5,0.7,0.5])
print(auc(y_true,y_pred))
# print(auc_0(y_true,y_pred))
print(auc_2(y_true,y_pred))